<template>
  <div class="login">
    <div class="head"></div>
    <div class="title">智能网联汽车大数据云控基础平台</div>
    <div class="login_input">
      <div class="login_title">登录</div>
      <a-form
        :model="form"
        layout="vertical">
        <a-form-item name="username">
          <template v-slot:label>
            <div class="login_label">
              <img src="@/assets/images/login/account.png">
              <span>账户</span>
            </div>
          </template>
          <a-input
            v-model:value="form.username"
            autocomplete="off"
            placeholder="输入账户"
            size="large"
            @change="errorTip.show = false"/>
        </a-form-item>
        <a-form-item name="password">
          <template v-slot:label>
            <div class="login_label">
              <img src="@/assets/images/login/password.png">
              <span>密码</span>
            </div>
          </template>
          <a-input-password
            v-model:value="form.password"
            autocomplete="off"
            placeholder="输入密码"
            size="large"
            @change="errorTip.show = false"/>
        </a-form-item>
      </a-form>
      <div
        v-show="errorTip.show"
        class="error_tip">
        <span class="error_img">!</span>
        {{ errorTip.tip }}
      </div>
      <a-button
        class="login_btn"
        :disabled="!form.username || !form.password"
        size="large"
        type="primary"
        @click="confirm">登录</a-button>
    </div>
  </div>
</template>
<script setup>
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import { login } from '@/api/login';
import md5 from 'md5';

const form = reactive({
  username: undefined,
  password: undefined
});

const errorTip = reactive({
  show: false,
  tip: '账户与密码不匹配'
});

const router = useRouter();

const confirm = () => {
  const { username, password } = form;
  login({ username, password: md5(password) }).then(({ code, msg, data }) => {
    if (code === 0) {
      sessionStorage.setItem('token', data.token);
      debugger
      const source = sessionStorage.getItem('source');
      if(source && source.startsWith('http')){
        if(source.includes('customer')){
          window.location.href = source+'?phone='+data.user.phone;
        }else{
          window.location.href = source+'?token='+data.token;
        }
      }else{
        router.replace({ path: '/guide' });
      }
    } else {
      errorTip.tip = msg;
      errorTip.show = true;
    }
  })
  .catch(err =>{
    errorTip.tip = '请求异常';
    errorTip.show = true;
  })
};

</script>
<style scoped>
.login {
  position: relative;
  display: flex;
  width: 100%;
  height: 100%;
  align-items: center;
  background-image: url("../../assets/images/login/login_bg.jpeg");
  background-size: 100% 100%;
}
.head {
  position: absolute;
  top: 16px;
  left: 16px;
  right: 0;
  display: flex;
  height: 88px;
  background-repeat: no-repeat;
  padding-left: 12px;
  align-items: center;
  justify-content: flex-start;
  background-size: auto 50%;
  background-image: url(../../assets/images/logo.png);
}
.title {
  color: white;
  position: absolute;
  right: calc(50% - 60px);
  font-size: 48px;
  font-weight: 700;
}
.head > span {
  margin-left: 10px;
  color: #fff;
  font-size: 24px;
}
.head > img {
  width: 120px;
}
.login_input {
  position: relative;
  margin-left: calc(50% + 100px);
  width: 500px;
  height: 580px;
  padding: 40px;
  background: #fff;
}
.login_title {
  height: 80px;
  line-height: 80px;
  text-align: center;
  font-size: 32px;
  color: black;
}
.login_label {
  display: flex;
  height: 32px;
  align-items: center;
  justify-content: flex-start;
}

.login_label > span {
  margin-left: 10px;
}
.error_tip {
  display: flex;
  height: 20px;
  color: red;
  justify-content: flex-end;
  align-items: center;
}
.login_btn {
  position: absolute;
  bottom: 158px;
  left: 40px;
  width: calc(100% - 80px);
}
.error_img {
  display: inline-block;
  margin-right: 10px;
  height: 16px;
  width: 16px;
  border-radius: 8px;
  line-height: 16px;
  background: red;
  color: #fff;
  text-align: center;
}
.ant-btn-primary[disabled] {
  background-color: #82aaec;
  color: #fff;
}
</style>
